﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MidimediaFinder.Resources;
using System.IO;
using System.Threading;
using Microsoft.Phone.Tasks;
using System.Xml.Serialization;
using System.Device.Location;
using System.Collections.ObjectModel;

namespace MidimediaFinder
{
    public partial class MainPage : PhoneApplicationPage
    {
        public static string hostname = "http://midimedia.altervista.org/";
        public static List<Ads> ads;
        bool working = false;
        List<Pair> distances;
        List<Pair> intervals;
        public static SavedOptions savedOptions;
        public static Result result;
        public static List<type> tipes;
        GeoCoordinateWatcher GeoCoordinateWatcher = new GeoCoordinateWatcher();
        GeoPositionStatus GpsStatus = GeoPositionStatus.NoData;

        private void WatcherStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            this.GpsStatus = e.Status;
        }

        public MainPage()
        {
            /// Holds the push channel that is created or found.

            PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
            InitializeComponent();
            DataContext = App.ViewModel;
            Ads_List.DataContext = App.ViewModel;
            GeoCoordinateWatcher.StatusChanged += WatcherStatusChanged;

            try
            {
                var occ = Storage.LoadUtilizations() + 1;
                new Thread(new ThreadStart(() =>
                {
                    Storage.getHTML(Storage.GetUtilizationUrl(occ, App.isTrial()),
                        (s) =>
                        {
                            Storage.DeleteUtilizations();
                        },
                        () =>
                        {
                            Storage.SaveUtilizations(occ);
                        });
                })).Start();
            }
            catch { }


            ads = Storage.LoadAds();
            App.ViewModel.Ads.Clear();
            for (int i = 0; i < ads.Count; i++)
                App.ViewModel.Ads.Add(new AdsViewModel()
                {
                    ID = ads[i].ID,
                    Description = ads[i].Description,
                    ImageUri = new Uri(ads[i].ImageUrl, UriKind.RelativeOrAbsolute),
                    Name = ads[i].Name
                }
                    );
            UpdateAds();
            UpdateTipes();
            //SetElements();


            BuildApplicationBar();

            distances = Storage.LoadDistances();
            for (int i = 0; i < distances.Count; i++)
            {
                var b = new MenuItem() { Header = distances[i].header };
                b.Click += DistanceChooser_Click;
                DistanceChooser_Border_ContextMenu.Items.Add(b);
            }

            intervals = Storage.LoadIntervals();
            for (int i = 0; i < intervals.Count; i++)
            {
                var b = new MenuItem() { Header = intervals[i].header };
                b.Click += IntervalChooser_Click;
                IntervalChooser_Border_ContextMenu.Items.Add(b);
            }

            savedOptions = Storage.LoadSavedOptions();

            if (savedOptions.isFirstStart)
            {
                savedOptions.isFirstStart = false;
                savedOptions.gpsPolicyAccepted = MessageBox.Show(Strings.GPS_Alert_Message, Strings.GPS_Alert_Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK;
                Storage.SaveSavedOptions(savedOptions);
            }
            savedOptions.gps = savedOptions.gps && savedOptions.gpsPolicyAccepted;

            if (savedOptions.interval < 0 || savedOptions.interval >= intervals.Count)
                savedOptions.interval = 0;

            if (savedOptions.distance < 0 || savedOptions.distance >= distances.Count)
                savedOptions.distance = 0;

            //IntervalChooser_Area.Text = intervals[savedOptions.interval].header;
            IntervalChooser_Area.Content = intervals[savedOptions.interval].header;
            //DistanceChooser_Area.Text = distances[savedOptions.distance].header;
            DistanceChooser_Area.Content = distances[savedOptions.distance].header;

            Address_Area.Text = savedOptions.address;

            Notifications_Email_Area.Text = savedOptions.email;

            if (savedOptions.email != "")
            {
                Notifications_Button.Content = Strings.Notifications_update_email_adress;
            }


            Notifications_Switch.IsChecked = savedOptions.notificationsEnabled;
            

            if (!savedOptions.gps)
            {
                GPS_Switch.IsChecked = false;
                Address_Area.Visibility = System.Windows.Visibility.Visible;
                Address_Label.Visibility = System.Windows.Visibility.Visible;
            }

        }

        public void BuildApplicationBar()
        {
            ApplicationBar.Buttons.Clear();
            ApplicationBarIconButton credits = new ApplicationBarIconButton() { Text = Strings.Credits, IsEnabled = true };
            if (App.CurrentTheme == App.Theme.Dark)
                credits.IconUri = new Uri("\\Images\\dark\\appbar.questionmark.rest.png", UriKind.Relative);
            else
                credits.IconUri = new Uri("\\Images\\light\\appbar.questionmark.rest.png", UriKind.Relative);
            credits.Click += OpenCredits_Button_Click;
            ApplicationBar.Buttons.Add(credits);
        }
        public void UpdateAds()
        {

            try
            {

                var url = "http://fboscari.altervista.org/GetAds.php" + "?name=" + Strings.Title + "&lang=" + Strings.Lang;
                Storage.getHTML(url,
                    s =>
                    {
                        AdsParser parser = new AdsParser(s);
                        var newList = parser.getAds();
                        if (newList.Count <= 0)
                            return;
                        ads = newList;
                        Dispatcher.BeginInvoke(() =>
                        {
                            Storage.SaveAds(ads);
                            App.ViewModel.Ads.Clear();
                            for (int i = 0; i < ads.Count; i++)
                                App.ViewModel.Ads.Add(new AdsViewModel()
                                {
                                    ID = ads[i].ID,
                                    Description = ads[i].Description,
                                    ImageUri = new Uri(ads[i].ImageUrl, UriKind.RelativeOrAbsolute),
                                    Name = ads[i].Name
                                });
                        });
                    },
                    () =>
                    {

                    });

            }
            catch { }
        }

        public void UpdateTipes()
        {

            try
            {

                var url = "http://midimedia.altervista.org/GetTypes.php?xml=1" + "&lang=" + Strings.Lang;
                Storage.getHTML(url,
                    s =>
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(Tipes));
                        var reader = new StringReader(s);
                        Tipes result = serializer.Deserialize(reader) as Tipes;
                        tipes = result.tipes;
                        if (savedOptions.type < 0 || savedOptions.type >= tipes.Count)
                            savedOptions.type = 0;
                        if (savedOptions.type >= 0 && savedOptions.type < tipes.Count)
                            Dispatcher.BeginInvoke(() =>
                            {
                                TypeChooser_Button.Content = tipes[savedOptions.type].Name;
                            });
                        App.ViewModel.Types.Clear();
                        for (int i = 0; i < tipes.Count; i++)
                            App.ViewModel.Types.Add(new TipesViewModel() { Index = i, Name = tipes[i].Name });
                    },
                    () =>
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show(Strings.ConnectionError);
                        });
                    });

            }
            catch { }
        }

        private void OpenCredits_Button_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/CreditsPage.xaml", UriKind.Relative));
        }

        public void ToggleOverlay(bool isOpen)
        {
            working = isOpen;
            if (isOpen)
            {
                CancelUpdate_Button.Visibility = System.Windows.Visibility.Visible;

                EnableProgressBar();

                ApplicationBar.IsVisible = false;

                GPS_Switch.Visibility = System.Windows.Visibility.Collapsed;

                DistanceChooser_Area.Visibility = System.Windows.Visibility.Collapsed;
                DistanceChooser_Border.Visibility = System.Windows.Visibility.Collapsed;
                DistanceChooser_Border_ContextMenu.Visibility = System.Windows.Visibility.Collapsed;
                DistanceChooser_Label.Visibility = System.Windows.Visibility.Collapsed;

                IntervalChooser_Area.Visibility = System.Windows.Visibility.Collapsed;
                IntervalChooser_Border.Visibility = System.Windows.Visibility.Collapsed;
                IntervalChooser_Border_ContextMenu.Visibility = System.Windows.Visibility.Collapsed;
                IntervalChooser_Label.Visibility = System.Windows.Visibility.Collapsed;

                Address_Area.Visibility = System.Windows.Visibility.Collapsed;
                Address_Label.Visibility = System.Windows.Visibility.Collapsed;

                Search_Button.Visibility = System.Windows.Visibility.Collapsed;

                TypeChooser_Button.Visibility = System.Windows.Visibility.Collapsed;
                TypeChooser_Label.Visibility = System.Windows.Visibility.Collapsed;

            }
            else
            {
                CancelUpdate_Button.Visibility = System.Windows.Visibility.Collapsed;
                DisableProgressBar();

                ApplicationBar.IsVisible = true;

                GPS_Switch.Visibility = System.Windows.Visibility.Visible;

                DistanceChooser_Area.Visibility = System.Windows.Visibility.Visible;
                DistanceChooser_Border.Visibility = System.Windows.Visibility.Visible;
                DistanceChooser_Border_ContextMenu.Visibility = System.Windows.Visibility.Visible;
                DistanceChooser_Label.Visibility = System.Windows.Visibility.Visible;

                IntervalChooser_Area.Visibility = System.Windows.Visibility.Visible;
                IntervalChooser_Border.Visibility = System.Windows.Visibility.Visible;
                IntervalChooser_Border_ContextMenu.Visibility = System.Windows.Visibility.Visible;
                IntervalChooser_Label.Visibility = System.Windows.Visibility.Visible;

                if (savedOptions.gps)
                {
                    Address_Area.Visibility = System.Windows.Visibility.Collapsed;
                    Address_Label.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    Address_Area.Visibility = System.Windows.Visibility.Visible;
                    Address_Label.Visibility = System.Windows.Visibility.Visible;
                }
                Search_Button.Visibility = System.Windows.Visibility.Visible;

                TypeChooser_Button.Visibility = System.Windows.Visibility.Visible;
                TypeChooser_Label.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void DistanceChooser_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var t = (MenuItem)sender;
                String s = (String)(t.Header);


                //DistanceChooser_Area.Text = s;
                DistanceChooser_Area.Content = s;

                for (int i = 0; i < distances.Count; i++)
                    if (s == distances[i].header)
                    {
                        savedOptions.distance = i;
                        break;
                    }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void IntervalChooser_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var t = (MenuItem)sender;
                String s = (String)(t.Header);

                //currentAccount.number = s;
                //IntervalChooser_Area.Text = s;
                IntervalChooser_Area.Content = s;
                for (int i = 0; i < intervals.Count; i++)
                    if (s == intervals[i].header)
                    {
                        savedOptions.interval = i;
                        break;
                    }
                //BeginContractSearch();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void TypeChooser_Click(object sender, RoutedEventArgs e)
        {

            NavigationService.Navigate(new Uri("/TypesPage.xaml", UriKind.Relative));

        }

        public void OnError(string message)
        {
            Dispatcher.BeginInvoke(() =>
            {
                //Username_Area.IsEnabled = true;
                working = false;
                ToggleOverlay(false);

                DisableProgressBar();
                MessageBox.Show(message);

            });
        }

        private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            try
            {
                Border border = sender as Border;
                ContextMenu contextMenu = ContextMenuService.GetContextMenu(border);
                if (contextMenu.Parent == null)
                {
                    contextMenu.IsOpen = true;
                }
            }
            catch (Exception)
            { }
        }


        private void Address_Area_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                BeginSearch();
            }
        }

        private void EnableProgressBar()
        {
            Dispatcher.BeginInvoke(() =>
            {
                ToggleProgressBar.IsEnabled = true;
                ToggleProgressBar.IsIndeterminate = true;
                ToggleProgressBar.Visibility = System.Windows.Visibility.Visible;
            });
        }
        private void DisableProgressBar()
        {
            Dispatcher.BeginInvoke(() =>
            {
                ToggleProgressBar.IsEnabled = false;
                ToggleProgressBar.IsIndeterminate = false;
                ToggleProgressBar.Visibility = System.Windows.Visibility.Collapsed;
            });
        }

        private void CancelUpdate_Button_Click(object sender, RoutedEventArgs e)
        {
            ToggleOverlay(false);
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (working)
            {
                CancelUpdate_Button_Click(null, null);
                e.Cancel = true;
            }
        }
        private void GPS_Switch_Checked(object sender, RoutedEventArgs e)
        {
            if (!savedOptions.gpsPolicyAccepted)
            {
                savedOptions.gpsPolicyAccepted = MessageBox.Show(Strings.GPS_Alert_Message, Strings.GPS_Alert_Title, MessageBoxButton.OKCancel) == MessageBoxResult.OK;
                if (savedOptions.gpsPolicyAccepted)
                {
                    Storage.SaveSavedOptions(savedOptions);
                }
                else
                {
                    GPS_Switch.IsChecked = false;
                    return;
                }
            }
            savedOptions.gps = true;
            Address_Area.Visibility = System.Windows.Visibility.Collapsed;
            Address_Label.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void GPS_Switch_Unchecked(object sender, RoutedEventArgs e)
        {
            savedOptions.gps = false;
            Address_Area.Visibility = System.Windows.Visibility.Visible;
            Address_Label.Visibility = System.Windows.Visibility.Visible;
        }
        private void OpenMarket()
        {
            if (!App.isTrial())
                return;

            MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
            marketplaceDetailTask.Show();
        }


        private void Ads_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Ads_List.SelectedItem == null)
                return;
            AdsViewModel ads = Ads_List.SelectedItem as AdsViewModel;
            Ads_List.SelectedItem = null;
            if (ads.Name != Strings.OtherProducts && ads.ID != "")
                OpenMarket(ads.ID);


        }
        private void OpenMarket(string ID)
        {

            MarketplaceDetailTask marketplaceDetailTask = new MarketplaceDetailTask();
            marketplaceDetailTask.ContentIdentifier = ID;
            marketplaceDetailTask.Show();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (savedOptions != null && tipes != null && savedOptions.type < tipes.Count)
                TypeChooser_Button.Content = tipes[savedOptions.type].Name;
        }

        private void Search_Button_Click(object sender, RoutedEventArgs e)
        {
            BeginSearch();
        }

        private void BeginSearch()
        {
            //string hostname = "http://midimedia.altervista.org/";
            
            //var url = "http://midimedia.altervista.org/SearchPOI.php?" + "?name=" + Strings.Title + "&lang=" + Strings.Lang;
            //var url = "http://midimedia.altervista.org/SearchPOI.php?lat=45.66018&log=12.24599&range=1000&interval=1&xml=1";

            if (tipes == null || tipes.Count == 0)
            {
                MessageBox.Show(Strings.ConnectionError);
                return;
            }

            var url = "http://midimedia.altervista.org/SearchPOI.php?xml=1";
            url += "&range=" + distances[savedOptions.distance].val;
            url += "&interval=" + intervals[savedOptions.interval].val;
            url += "&type=" + tipes[savedOptions.type].Id;
            url += "&lang=" + Strings.Lang;
            var adress = Address_Area!=null?Address_Area.Text:"";
            if (savedOptions.gps)
            {
                /*if (GpsStatus != GeoPositionStatus.Ready && GeoCoordinateWatcher.Status!= GeoPositionStatus.Ready)
                {
                    MessageBox.Show(Strings.GPSError);
                    return;
                }*/
            }

            ToggleOverlay(true);
            new Thread(new ThreadStart(() =>
            {
                if (savedOptions.gps)
                {

                    bool ok = GeoCoordinateWatcher.TryStart(false, new TimeSpan(0, 0, 10));
                    if (ok)
                    {
                        var l = GeoCoordinateWatcher.Position.Location;
                        url += "&lat=" + l.Latitude;
                        url += "&log=" + l.Longitude;
                    }
                    else
                    {

                        
                        Dispatcher.BeginInvoke(() =>
                        {
                            MessageBox.Show(Strings.GPSError);
                            ToggleOverlay(false);
                        });
                        return;
                    }
                }
                else
                {
                    url += "&adress=" + adress;
                }
                url += "&uid=" + Storage.getID();
                //Console.WriteLine(url);
                url += "&salt=" + DateTime.Now.Millisecond;
                url = url.Replace(',', '.');
                Storage.getHTML(url,
                    s =>
                    {
                        if (!working)
                            return;
                        Result result = null;

                        XmlSerializer serializer = new XmlSerializer(typeof(Result));
                        var reader = new StringReader(s);
                        result = serializer.Deserialize(reader) as Result;

                        if (result == null)
                            return;
                        result.UpdateChars();
                        Dispatcher.BeginInvoke(() =>
                        {
                            ToggleOverlay(false);
                        });
                        //Console.WriteLine(result.ToString());
                        if (result.items == null || result.items.Count == 0)
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                MessageBox.Show(Strings.NoResults);
                            });
                        }
                        else if (result.err == "1" && result.message == "adressresolveerror")
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                MessageBox.Show(Strings.AdressResolveError);
                            });
                        }
                        else if (result.err == "1" && result.message == "missingdata")
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                MessageBox.Show(Strings.MissingData);
                            });
                        }
                        else
                        {
                            Dispatcher.BeginInvoke(() =>
                            {
                                MainPage.result = result;
                                savedOptions.address = Address_Area.Text;
                                Storage.SaveSavedOptions(savedOptions);
                                //App.ViewModel.Items = result.items;
                                App.ViewModel.Items.Clear();
                                //var plain_star = new Uri("/MidimediaFinder;component/Images/star.png", UriKind.Absolute);
                                var plain_star = new Uri("\\star.png", UriKind.Relative);
                                var half_star = new Uri("\\star2.png", UriKind.Relative);
                                for (int i = 0; i < result.items.Count; i++)
                                {
                                    var item = result.items[i];
                                    item.ImageUrl = hostname + item.ImageUrl;
                                    var entry = new ItemViewModel()
                                    {
                                        _Name = item.Name,
                                        _Distance = item.Distance + " KM",
                                        _ImageUrl = new Uri(item.ImageUrl, UriKind.RelativeOrAbsolute),
                                        _Item = item
                                    };

                                    if (item.Stars >= 5)
                                        entry._Star5 = plain_star;
                                    else if (item.Stars >= 4.5)
                                        entry._Star5 = half_star;

                                    if (item.Stars >= 4)
                                        entry._Star4 = plain_star;
                                    else if (item.Stars >= 3.5)
                                        entry._Star4 = half_star;

                                    if (item.Stars >= 3)
                                        entry._Star3 = plain_star;
                                    else if (item.Stars >= 2.5)
                                        entry._Star3 = half_star;

                                    if (item.Stars >= 2)
                                        entry._Star2 = plain_star;
                                    else if (item.Stars >= 1.5)
                                        entry._Star2 = half_star;

                                    if (item.Stars >= 1)
                                        entry._Star1 = plain_star;
                                    else if (item.Stars >= 0.5)
                                        entry._Star1 = half_star;

                                    App.ViewModel.Items.Add(entry);


                                }

                                App.ViewModel.Promotions.Clear();
                                for (int i = 0; i < result.promotions.Count; i++)
                                {
                                    var promotion = result.promotions[i];
                                    promotion.ImageUrl = hostname + promotion.ImageUrl;
                                    promotion.Icon = hostname + promotion.Icon;
                                    var entry = new PromotionViewModel()
                                    {
                                        _Name = promotion.Short_description,
                                        _Distance = promotion.Distance + " KM",
                                        _ImageUrl = new Uri(promotion.ImageUrl, UriKind.RelativeOrAbsolute),
                                        _Item = promotion,
                                        _Hint = promotion.Hint,
                                        _Icon = new Uri(promotion.Icon, UriKind.RelativeOrAbsolute),
                                    };
                                    App.ViewModel.Promotions.Add(entry);
                                }

                                NavigationService.Navigate(new Uri("/ResultsPage.xaml", UriKind.Relative));
                            });
                        }
                    },
                    () =>
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            ToggleOverlay(false);
                            MessageBox.Show(Strings.ConnectionError);
                        });
                    });
            })).Start();
        }

        Boolean notificationIsWorking = false;

        private void Notifications_Email_Area_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SendEmailData();
            }
        }

        private void Notifications_Button_Click(object sender, RoutedEventArgs e)
        {
            SendEmailData();
        }

        private void Notifications_CancelUpdate_Button_Click(object sender, RoutedEventArgs e)
        {
            notificationIsWorking = false;
            SetNotificationsElementsVisibility(true);
        }

        private void SendEmailData()
        {
            if (Notifications_Email_Area.Text == "")
            {
                MessageBox.Show(Strings.Notifications_Missing_Data);
                return;
            }

            notificationIsWorking = true;
            SetNotificationsElementsVisibility(false);

            String url = hostname + "SendNotificationsData.php?xml=1";
            url += "&enabled=" + (Notifications_Switch.IsChecked.Value ? "1" : "0");
		    url += "&adress=" + Notifications_Email_Area.Text;
            url += "&phoneid=" + Storage.getID();

            url += "&salt=" + DateTime.Now.Millisecond;

            Storage.getHTML(url,
                s =>
                {
                    
                    Dispatcher.BeginInvoke(() =>
                    {
                        SetNotificationsElementsVisibility(true);
                        if (notificationIsWorking)
                        {
                            savedOptions.email = Notifications_Email_Area.Text;
                            savedOptions.notificationsEnabled = Notifications_Switch.IsChecked.Value;
                            Storage.SaveSavedOptions(savedOptions);
                            MessageBox.Show(Strings.Notifications_preferences_saved);
                            Notifications_Button.Content = Strings.Notifications_update_email_adress;
                        }
                        notificationIsWorking = false;
                    });
                    
                },
                () =>
                {
                    notificationIsWorking = false;
                    Dispatcher.BeginInvoke(() =>
                    {
                        SetNotificationsElementsVisibility(true);
                        MessageBox.Show(Strings.ConnectionError);
                    });
                });
        }

        public void SetNotificationsElementsVisibility(Boolean visible)
        {
            if (visible)
            {
                Notifications_Button.Visibility = System.Windows.Visibility.Visible;
                Notifications_Desc.Visibility = System.Windows.Visibility.Visible;
                Notifications_Label.Visibility = System.Windows.Visibility.Visible;
                Notifications_Switch.Visibility = System.Windows.Visibility.Visible;
                Notifications_Email_Area.Visibility = System.Windows.Visibility.Visible;

                Notifications_ToggleProgressBar.Visibility = System.Windows.Visibility.Collapsed;
                Notifications_CancelUpdate_Button.Visibility = System.Windows.Visibility.Collapsed;

                Notifications_ToggleProgressBar.IsEnabled = false;
                Notifications_ToggleProgressBar.IsIndeterminate = true;
            }
            else
            {
                Notifications_Button.Visibility = System.Windows.Visibility.Collapsed;
                Notifications_Desc.Visibility = System.Windows.Visibility.Collapsed;
                Notifications_Label.Visibility = System.Windows.Visibility.Collapsed;
                Notifications_Switch.Visibility = System.Windows.Visibility.Collapsed;
                Notifications_Email_Area.Visibility = System.Windows.Visibility.Collapsed;

                Notifications_ToggleProgressBar.Visibility = System.Windows.Visibility.Visible;
                Notifications_CancelUpdate_Button.Visibility = System.Windows.Visibility.Visible;
                Notifications_ToggleProgressBar.IsEnabled = true;
                Notifications_ToggleProgressBar.IsIndeterminate = true;
            }
        }
    }


}